import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { UserEntity } from 'src/user/entities/user.entity';

@Injectable()
export class AuthService {
  constructor(private readonly jwtService: JwtService) {}

  // 生成 token
  async certificate(user) {
    return this.jwtService.sign(user);
  }

  // 登录
  async login(user: UserEntity) {
    const token = await this.certificate({
      username: user.username,
      id: user.id,
      role: user.role,
      nickname: user.nickname,
    });
    return { token, nickname: user.nickname, username: user.username, id: user.id, role: user.role };
  }
}
